package cn.tedu.role.controller;

import cn.tedu.role.pojo.dto.RoleDTO;
import cn.tedu.role.pojo.dto.RoleQueryDTO;
import cn.tedu.role.pojo.dto.UpdateRuleDTO;
import cn.tedu.role.pojo.vo.RoleVO;
import cn.tedu.role.service.RoleService;
import cn.tedu.role.utils.JsonResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/role/")
@Slf4j
@Api(tags = "角色组")
@Validated
public class RoleController {
    @Autowired
    private RoleService roleService;

    @ApiOperation("列表")
    @GetMapping("list")
    public JsonResult list(RoleQueryDTO roleQueryDTO) {
        log.debug("获取参数:{}", roleQueryDTO);
        IPage<RoleVO> data = roleService.select(roleQueryDTO);
        return JsonResult.ok(data);
    }

    @ApiOperation("添加/更新")
    @PostMapping("updateOrInsert")
    public JsonResult updateOrInsert(@Validated @RequestBody RoleDTO roleDTO) {
        roleService.updateAOrInsert(roleDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除")
    @PostMapping("delete/{id}")
    public JsonResult delete(@PathVariable("id") Integer id) {
        roleService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("更改状态")
    @PostMapping("changeStatus/{id}")
    public JsonResult changeStatus(@PathVariable("id") Integer id) {
        roleService.changeStatus(id);
        return JsonResult.ok();
    }

    @ApiOperation("获取权限组")
    @GetMapping("getRules")
    public JsonResult getRules(Integer id) {
        List<String> data = roleService.getRules(id);
        return JsonResult.ok(data);
    }

    @ApiOperation("分配权限")
    @PostMapping("updateRule")
    public JsonResult updateRule(@RequestBody UpdateRuleDTO updateRuleDTO) {
        roleService.updateRule(updateRuleDTO);
        return JsonResult.ok();
    }
}
